const {
  addRole,
  getRoleById,
  deleteRole,
  updateRole,
  getRoles,
} = require("@/models/admin/role-model");

/**
 * 添加角色
 */
exports.append = async (req, res, next) => {
  try {
    if (!req.body.name) throw new Error("请输入角色名称");
    const result = await addRole(req.body);
    res.backSuccess(result);
  } catch (error) {
    next(error);
  }
};

/**
 * 删除角色
 */
exports.remove = async (req, res, next) => {
  try {
    var result = await getRoleById({ id: req.params.id });
    if (!result) throw new Error("角色不存在");
    result = await deleteRole(req.params.id);
    res.backSuccess(result);
  } catch (error) {
    next(error);
  }
};

/**
 * 编辑角色
 */
exports.edit = async (req, res, next) => {
  try {
    if (!req.body.name) throw new Error("请输入角色名称");
    const result = await updateRole(req.params.id, req.body);
    res.backSuccess(result);
  } catch (error) {
    next(error);
  }
};

/**
 * 查询角色
 */
exports.page = async (req, res, next) => {
  try {
    const result = await getRoles(req.query);
    res.backSuccess(result);
  } catch (error) {
    next(error);
  }
};

/**
 * 角色详情
 */
exports.detail = async (req, res, next) => {
  try {
    const result = await getRoleById({ id: req.params.id });
    res.backSuccess(result);
  } catch (error) {
    next(error);
  }
};
